package br.com.sgi.util;



import java.awt.Color;
import java.util.Date;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.design.JRDesignBand;
import net.sf.jasperreports.engine.design.JRDesignExpression;
import net.sf.jasperreports.engine.design.JRDesignField;
import net.sf.jasperreports.engine.design.JRDesignStaticText;
import net.sf.jasperreports.engine.design.JRDesignTextField;
import net.sf.jasperreports.engine.design.JasperDesign;


public class ReportDesigner {


	
  public JasperDesign getDesign() throws JRException {

    // Cria um novo relatorio
    JasperDesign jasperDesign = new JasperDesign();

    // PAGE
    configPage(jasperDesign);

    // FIELD

    JRDesignField field = null;
    addField("nome", String.class, jasperDesign);
    addField("matricula", String.class, jasperDesign);
    addField("dtNascimento", Date.class, jasperDesign);

    
   
    
    // TITLE

    JRDesignBand jrDesignband = null;
    JRDesignStaticText staticText = null;
    configTitle(jasperDesign);

    // PAGE HEADER

    jrDesignband = new JRDesignBand();
    jrDesignband.setHeight(15);
    jasperDesign.setPageHeader(jrDesignband);

    // COLUMN HEADER

    jrDesignband = new JRDesignBand();
    jrDesignband.setHeight(25);
    jasperDesign.setColumnHeader(jrDesignband);

    //Dados da referencia
    staticText = new JRDesignStaticText();
    staticText.setX(0);
    staticText.setY(5);
    staticText.setWidth(30);
    staticText.setHeight(15);
    //staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
    staticText.setHorizontalAlignment((byte)1);
    staticText.setText("Matrícula");
    staticText.setBold(true);
    jrDesignband.addElement(staticText);

    //Dados do consumo
    staticText = new JRDesignStaticText();
    staticText.setX(40);
    staticText.setY(5);
    staticText.setWidth(30);
    staticText.setHeight(15);
    //staticText.setHorizontalAlignment(HorizontalAlignEnum.RIGHT);
    staticText.setHorizontalAlignment((byte)3);
    staticText.setText("Nome");
    staticText.setBold(true);
    jrDesignband.addElement(staticText);
    
    //Dados do consumo
    staticText = new JRDesignStaticText();
    staticText.setX(150);
    staticText.setY(5);
    staticText.setWidth(150);
    staticText.setHeight(15);
    //staticText.setHorizontalAlignment(HorizontalAlignEnum.RIGHT);
    staticText.setHorizontalAlignment((byte)3);
    staticText.setText("Data Nascimento");
    staticText.setBold(true);
    jrDesignband.addElement(staticText);

    // DETAIL
    jrDesignband = new JRDesignBand();
    jrDesignband.setHeight(20);

    
    
    //Dados da referencia
    JRDesignTextField textField = new JRDesignTextField();
    textField.setBlankWhenNull(false);
    textField.setX(0);
    textField.setY(5);
    textField.setWidth(30);
    textField.setHeight(15);
    // Adiciona a expressao no campo
    JRDesignExpression expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("$F{matricula}");
    textField.setExpression(expression);
    jrDesignband.addElement(textField);

    //Dados do consumo
    textField = new JRDesignTextField();
    textField.setBlankWhenNull(false);
    textField.setX(40);
    textField.setY(5);
    textField.setWidth(30);
    textField.setHeight(15);
   // textField.setHorizontalAlignment(HorizontalAlignEnum.RIGHT);
    staticText.setHorizontalAlignment((byte)3);
    //Adiciona a expressao no campo
    expression = new JRDesignExpression();
    expression.setValueClass(java.lang.String.class);
    expression.setText("$F{nome}");
    textField.setExpression(expression);
    jrDesignband.addElement(textField);
    
    //Dados do consumo
    textField = new JRDesignTextField();
    textField.setBlankWhenNull(false);
    textField.setX(150);
    textField.setY(5);
    textField.setWidth(150);
    textField.setHeight(15);
   // textField.setHorizontalAlignment(HorizontalAlignEnum.RIGHT);
    staticText.setHorizontalAlignment((byte)3);
    //Adiciona a expressao no campo
    expression = new JRDesignExpression();
    expression.setValueClass(java.util.Date.class);
    expression.setText("$F{dtNascimento}");
    textField.setExpression(expression);
    jrDesignband.addElement(textField);

    jasperDesign.setDetail(jrDesignband);

    // COLUMN FOOTER (Escondido)
    jrDesignband = new JRDesignBand();
    jrDesignband.setHeight(0);
    jasperDesign.setColumnFooter(jrDesignband);

    // PAGE FOOTER (Escondido)
    jrDesignband = new JRDesignBand();
    jrDesignband.setHeight(0);
    jasperDesign.setPageFooter(jrDesignband);

    // NO DATA (No JasperViewer nao mostra)
    jrDesignband = new JRDesignBand();
    jrDesignband.setHeight(20);
    staticText = new JRDesignStaticText();
    staticText.setX(10);
    staticText.setY(5);
    staticText.setWidth(64);
    staticText.setHeight(15);
    staticText.setText("Sem registros para compor o relatorio!");
   // staticText.setHorizontalAlignment(HorizontalAlignEnum.CENTER);
    staticText.setHorizontalAlignment((byte)2);
    jrDesignband.addElement(staticText);
    jasperDesign.setNoData(jrDesignband);

    // SUMMARY (Escondido)
    jrDesignband = new JRDesignBand();
    jrDesignband.setHeight(0);
    jasperDesign.setSummary(jrDesignband);

    return jasperDesign;
  }

  /**
   * Ajusta os parametros do titulo
   * @param jasperDesign
   */
  private void configTitle(JasperDesign jasperDesign) {
    JRDesignBand band = new JRDesignBand();
    band.setHeight(50);
    JRDesignStaticText staticText = new JRDesignStaticText();
    staticText.setX(10);
    staticText.setY(10);
    staticText.setWidth(450);
    staticText.setHeight(40);
    staticText.setFontName("Tahoma");
    staticText.setForecolor(Color.BLUE);
    //staticText.setHorizontalAlignment(HorizontalAlignEnum.CENTER);
    staticText.setHorizontalAlignment((byte)2);
    staticText.setFontSize(24);
    staticText.setPdfFontName("Helvetica-Bold");
    staticText.setBold(true);
    staticText.setText("Historico de Consumo");

    band.addElement(staticText);

    jasperDesign.setTitle(band);
  }

  /**
   * Adiciona campos ao relatorio
   * @param name
   * @param clazz
   * @param jasperDesign
   * @throws JRException
   */
  private void addField(String name, Class clazz, JasperDesign jasperDesign)
      throws JRException
  {
    JRDesignField field = new JRDesignField();

    field.setName(name);
    field.setValueClass(clazz);
    jasperDesign.addField(field);
  }

  /**
   * Configura a pagina
   * @param jasperDesign
   */
  private void configPage(JasperDesign jasperDesign) {
    jasperDesign.setPageWidth(500);
    jasperDesign.setPageHeight(1000);
    jasperDesign.setColumnCount(1);
    jasperDesign.setColumnWidth(450);
    jasperDesign.setColumnSpacing(0);
    jasperDesign.setLeftMargin(10);
    jasperDesign.setRightMargin(10);
    jasperDesign.setBottomMargin(5);
    jasperDesign.setTopMargin(5);
    jasperDesign.setName("RelatorioDeConsumo");

  }
}